﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class XemSuaChucVu : Form
    {
        public XemSuaChucVu()
        {
            InitializeComponent();
        }

        private void loadDataGridView()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChucVu");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["ChucVu"];
        }

        public void loadDataGridViewWithMSPB(string MSPB)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChucVu");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["ChucVu"];
        }

        private void loadCombobox()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            conn.Close();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PhongBan");
            PhongBanCombobox.DisplayMember = "Ten_PB";
            PhongBanCombobox.ValueMember = "MaSo_PB";
            PhongBanCombobox.DataSource = ds.Tables["PhongBan"].DefaultView;
        }

        private void docThongTin(string MSCV)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_CV = @MaSo_CV";
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_CV",MSCV);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MaSo_CV.Text = reader["MaSo_CV"].ToString();
                Ten_CV.Text =  reader["Ten_CV"].ToString();
                PhongBanCombobox.SelectedValue = reader["MaSo_PB"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void XemSuaChucVu_Load(object sender, EventArgs e)
        {
            loadDataGridView();
            loadCombobox();
        }

        private void capNhatChucVu(string MaSoCV, string TenCV, string MaSoPB)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update ChucVu set Ten_CV = @Ten_CV where MaSo_CV = @MaSo_CV AND MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_PB", MaSoPB);
            cmd.Parameters.AddWithValue("@Ten_CV", TenCV);
            cmd.Parameters.AddWithValue("@MaSo_CV", MaSoCV);
            cmd.ExecuteReader();
            conn.Close();
            nofication.Text = "Cập nhật thành công !";
        }

        private void CapNhatButton_Click(object sender, EventArgs e)
        {
            ThongBaoThayDoi f = new ThongBaoThayDoi();
            if (f.ShowDialog() == DialogResult.OK)
            {
                capNhatChucVu(MaSo_CV.Text, Ten_CV.Text, PhongBanCombobox.SelectedValue.ToString());
                Ten_CV.Enabled = false;
                PhongBanCombobox.Enabled = false;
                loadDataGridView();
                loadCombobox();
            }
            else
                f.Close();
        }

        public void xoaChucVu(string MSCV)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhanVien where MaSo_CV = @MaSo_CV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_CV", MSCV);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show("Chức vụ còn tồn tại trong bảng Nhân Viên", "Thông báo", MessageBoxButtons.OK);
                XemSuaNhanVien f = new XemSuaNhanVien();
                f.ShowDialog();
                reader.Close();
            }
            else
            {
                reader.Close();
                cmd.CommandText = "delete from ChucVu where MaSo_CV = @MaSo_CV";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@MaSo_CV", MSCV);
                cmd.ExecuteNonQuery();
            }
            conn.Close();

            //string sql = "delete from ChucVu where MaSo_CV = @MaSo_CV";
            //SqlCommand cmd = new SqlCommand(sql, conn);
            //conn.Open();
            //cmd.Parameters.AddWithValue("@MaSo_CV", MSCV);
            //cmd.ExecuteNonQuery();
            //conn.Close();
        }

        private void QuayLaiButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChinhSuaButton_Click(object sender, EventArgs e)
        {
            Ten_CV.Enabled = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSCV;
            MSCV = dataGridView1.Rows[e.RowIndex].Cells["MaSo_CV1"].Value.ToString();
            docThongTin(MSCV);
        }

        private void XoaButton_Click(object sender, EventArgs e)
        {
            ThongBaoXoa f = new ThongBaoXoa();
            if (f.ShowDialog() == DialogResult.OK)
            {
                xoaChucVu(MaSo_CV.Text);
                loadDataGridView();
                loadCombobox();
            }
            else
                f.Close();
        }
                
    }
}
